package xyj.sparrow.scheduler.cron.ops;

import xyj.sparrow.scheduler.cron.Cron;
import xyj.sparrow.scheduler.cron.stream.CronProvider;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * redis进行集群事件广播
 *
 * @author 江南小俊
 * @since 2021/6/22
 **/
public class RedisMessageSync implements MessageSync {
    @Resource
    private CronProvider cronProvider;
    private final String KEY = "channel:stream:cron";

    @Override
    public void removeByTaskIds(List<String> taskIds) {
        cronProvider.sendOpsMsg(KEY, new OpsMsg(OpsMsg.OpsType.REMOVE_TASK, taskIds));
    }

    @Override
    public void removeByAppIds(List<String> appIds) {
        cronProvider.sendOpsMsg(KEY, new OpsMsg(OpsMsg.OpsType.REMOVE_APP, appIds));
    }

    @Override
    public void addCron(Cron cron) {
        cronProvider.sendOpsMsg(KEY, new OpsMsg(OpsMsg.OpsType.ADD_CRON,
                Collections.singletonList(cron.getTask().getTaskId())));
    }

    @Override
    public void updateCron(Cron cron) {
        cronProvider.sendOpsMsg(KEY, new OpsMsg(OpsMsg.OpsType.UPDATE_CRON,
                Collections.singletonList(cron.getTask().getTaskId())));
    }

    @Override
    public void batchUpdateCronByAppId(List<String> appIds) {
        cronProvider.sendOpsMsg(KEY, new OpsMsg(OpsMsg.OpsType.UPDATE_CRON_APP, appIds));
    }

    @Override
    public void batchUpdateCronByTaskId(List<String> taskIds) {
        cronProvider.sendOpsMsg(KEY, new OpsMsg(OpsMsg.OpsType.UPDATE_CRON_TASK, taskIds));
    }

    @Override
    public void runCronByTask(String taskId) {
        cronProvider.sendOpsMsg(KEY, new OpsMsg(OpsMsg.OpsType.RUN_CRON_TASK, Collections.singletonList(taskId)));
    }
}
